#include "hal_cs_f61_tempsensor.h"

#include <stdint.h>

/* TODO
 * The function of temsensor is not finishing
 */

unsigned short TempSensorFactorADC = 28500;

void Temp_FactorInit(void)
{
	uint8_t dat[4] = {0xff,0xff,0xff};
	uint8_t temp;

	FMC_ReadPage (0x100F00, 4, dat); 

	temp = dat[0]+dat[1];

	if(temp == dat[2] && (dat[0] | dat[1])) 
	{
		TempSensorFactorADC = dat[1];
		TempSensorFactorADC <<= 8;
		TempSensorFactorADC |= dat[0];
	}
}

void HAL_TempSensor_Init(void)
{
	System_TempSensorInit(HCLK_Div_2 , ENABLE);
	Temp_FactorInit();
}

////////////////////////////////////////////////////////////////////////////////

float hal_temperature_read(void)
{
	uint16_t tempSensorADC;
	float ret;

	System_TempSensorStartChange();

	while(System_TempSensorBusy()) {}

	tempSensorADC = System_ReadTempSensorData();

	ret = (float)(TempSensorFactorADC - tempSensorADC) / 60 + 10;
	return ret; // directly return the tamprature value instead of adc val
}

